package cn.yj.relay.core.service.impl;

import cn.yj.relay.common.utils.StringUtils;
import cn.yj.relay.core.entity.Admin;
import cn.yj.relay.core.mapper.AdminMapper;
import cn.yj.relay.core.service.IAdminService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 永健
 * @since 2020-03-03 04:36
 */
@Service
@Transactional
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService
{
    @Override
    public Admin getUserByToken(String requestToken)
    {
        return getOne(new QueryWrapper<Admin>().lambda().eq(Admin::getToken, requestToken));
    }

    @Override
    public Admin login(String name, String password, String token)
    {
        Admin admin = getOne(new QueryWrapper<Admin>().lambda().eq(Admin::getLoginName, name).eq(Admin::getPassword, password));
        if (StringUtils.isNull(admin))
        {
            return null;
        }
        admin.setToken(token);
        saveOrUpdate(admin);
        return admin;
    }
}
